<template>
  <div class="review-topic">
    <InnerBreadcrumb v-if="!idTip" v-model="indexKey" :path="breadPath" :showBack="true"/>
    <div class="edu-page-warp" v-show="indexKey == 1">
      <!--批量驳回 弹框-->
      <edu-dialog
        title="批量驳回"
        width="45%"
        @close="onRejectClose"
        :visible="rejectDialogVisible"
      >
        <el-form
          :model="rejectForm"
          :rules="rejectRules"
          ref="rejectForm"
          label-position="right"
          label-width="100px"
          label-suffix="："
        >
          <el-form-item label="审核意见" prop="remark">
            <el-input
              v-model="rejectForm.remark"
              type="textarea"
              :rows="4"
              maxlength="100"
              class="search-item"
              show-word-limit
              placeholder="请输入"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="buttons">
          <el-button @click="onRejectClose">取消</el-button>
          <el-button type="danger" @click="onRejectSubmit">驳回</el-button>
        </div>
      </edu-dialog>
      <condition-card @reset="reset" @search="searchList" label-width="140px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="毕业年份">
              <el-date-picker
                class="search-item"
                :clearable="false"
                v-model="queryForm.condition.grade"
                @change="handleGradeChange"
                value-format="yyyy"
                type="year"
                placeholder="请选择"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="行政学院">
              <el-select
                v-model="queryForm.condition.faculty"
                filterable
                @change="handleCollegeChange"
                class="search-item"
                clearable
              >
                <el-option
                  v-for="item in source.studentColleges"
                  :key="item.collegeId"
                  :label="item.collegeIdI18n"
                  :value="item.collegeId"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="专业">
              <el-select
                v-model="queryForm.condition.subject"
                filterable
                class="search-item"
                clearable
              >
                <el-option
                  v-for="item in source.subjectOptions"
                  :key="item.subjectId"
                  :label="item.subjectName"
                  :value="item.subjectId"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="课题性质">
              <el-select
                v-model="queryForm.condition.subjectProperties"
                filterable
                class="search-item"
                clearable
              >
                <el-option
                  v-for="item in source.subjectNatureOptions"
                  :key="item.id"
                  :label="item.subjectProperties"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="审核状态">
              <el-select
                v-model="queryForm.condition.status"
                filterable
                class="search-item"
                clearable
              >
                <el-option
                  v-for="item in source.statusOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="课题名称">
              <el-input
                v-model="queryForm.condition.projectName"
                class="search-item"
                placeholder="请输入"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </condition-card>
      <el-card shadow="never">
        <div class="edu-page-title">审核任务书</div>
        <div class="edu-opertion-bar">
          <div class="float-left">
            <el-input
              v-model="queryForm.condition.studentName"
              clearable
              placeholder="输入学生学号、姓名搜索"
              style="width:200px"
              @keyup.enter.native="searchList"
            >
              <i
                slot="suffix"
                class="el-icon-search"
                style="margin-top:10px;cursor:pointer"
                @click="searchList"
              ></i>
            </el-input>
          </div>
          <div class="float-right">
            <el-button
              @click="onExport"
              v-loading.fullscreen.lock="loading"
              element-loading-text="Loading"
              element-loading-spinner="el-icon-loading"
              element-loading-background="rgba(255, 255, 255, .48)"
            >导出
            </el-button>
            <el-button type="danger" @click="onBatchReject">驳回</el-button>
            <el-button type="primary" @click="onBatchPass">通过</el-button>
          </div>
        </div>
        <el-table
          :data="taskList"
          size="mini"
          style="width:100%"
          @selection-change="handleSelectionChange"
          tooltip-effect="light"
        >
          <el-table-column type="selection" stripe/>
          <el-table-column label="毕业年份" fixed="left">
            <template slot-scope="scope">{{ scope.row.grade }}</template>
          </el-table-column>
          <el-table-column label="课题名称" fixed="left" show-overflow-tooltip="true">
            <template slot-scope="scope">{{scope.row.projectName}}</template>
          </el-table-column>
          <el-table-column label="课题性质" fixed="left" show-overflow-tooltip="true">
            <template slot-scope="scope">{{ scope.row.subjectProperties }}</template>
          </el-table-column>
          <el-table-column label="导师" fixed="left" show-overflow-tooltip="true">
            <template slot-scope="scope">{{ scope.row.teacherName }}</template>
          </el-table-column>
          <el-table-column label="学生" fixed="left" show-overflow-tooltip="true">
            <template slot-scope="scope">{{ scope.row.studentName }}</template>
          </el-table-column>
          <el-table-column label="行政学院" fixed="left" show-overflow-tooltip="true">
            <template slot-scope="scope">{{ scope.row.facultyI18n }}</template>
          </el-table-column>
          <el-table-column label="专业" fixed="left" show-overflow-tooltip="true">
            <template slot-scope="scope">{{ scope.row.subject }}</template>
          </el-table-column>
          <el-table-column label="方向" fixed="left" show-overflow-tooltip="true">
            <template slot-scope="scope">{{ scope.row.direction }}</template>
          </el-table-column>
          <edu-table-column label="审核状态" min-width="120">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.status === 'WaitCommit' || scope.row.status === 'Recall'"
                      type="info">草稿
              </el-tag>
              <el-tag v-else-if="scope.row.status === 'Process'" type="info">待审核</el-tag>
              <el-tag v-else-if="scope.row.status === 'Reject'" type="danger">已驳回</el-tag>
              <el-tag v-else-if="scope.row.status === 'End'" type="success">已通过</el-tag>
            </template>
          </edu-table-column>
          <el-table-column :label="$t('label.operation')" fixed="right">
            <template slot-scope="scope">
              <el-button
                type="text"
                @click="onReview(scope.row)"
                v-if="scope.row.status === 'Process'"
              >审核
              </el-button>
              <el-button
                type="text"
                v-if="scope.row.status !== 'Process'"
                @click="onView(scope.row)"
              >查看
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="edu-pagination">
          <div class="float-right">
            <el-pagination
              :current-page="queryForm.pageNum_"
              :page-sizes="[20,50,100,500,1000]"
              :page-size="queryForm.pageSize_"
              :total="queryForm.total"
              layout="total, sizes, prev, pager, next, jumper"
              style="float: right;margin:10px 60px 0 0;"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </div>
        </div>
      </el-card>
    </div>
    <div v-if="indexKey == 2">
      <TaskInfo
        ref="topicInfo"
        :studentInfo="studentInfo"
        :subjectType="subjectType"
        :actionType="actionType"
        :basicInfoForm="basicInfoForm"
        :progressData="progressData"
        @topic-reject="topicReject"
        @topic-pass="topicPass"
        @topic-cancel="topicCancel"
      ></TaskInfo>
    </div>
    <div v-if="indexKey == 3">
      <TaskInfo
        ref="topicInfo"
        :studentInfo="studentInfo"
        :subjectType="subjectType"
        :actionType="actionType"
        :basicInfoForm="basicInfoForm"
        :progressData="progressData"
        @topic-reject="topicReject"
        @topic-pass="topicPass"
        @topic-cancel="topicCancel"
      ></TaskInfo>
    </div>
  </div>
</template>
<script>
  import API from 'common/src/api/underGraduationDesign/reviewSubject'
  import {
    auditBatch,
    auditSingle,
    exportData,
    getAuditList,
    getExportStatus,
    getSubjectType,
    viewData
  } from 'common/src/api/underGraduationDesign/docTaskBook'
  import tools from 'common/src/views/underGraduationDesign/tools.js';
  import {approvelDetail} from "common/src/api/approval";
  import mixin from 'common/src/mixins/index'

  export default {
    name: 'TaskAudit',
    props: {
      dataInfo: {
        type: Object,
        default: {}
      },
      idTip: {
        type: String,
      },
    },
    mixins: [mixin],
    data() {
      return {
        api: API,
        queryForm: {
          pageNum_: 1,
          pageSize_: 20,
          condition: {
            grade: null,
            faculty: null,
            status: null,
            subjectProperties: null,
            projectName: null,
            studentName: null,
            subject: null
          }
        },
        loading: false,
        indexKey: this.$route.query.indexKey?this.$route.query.indexKey:1,
        taskList: [],
        source: {
          studentColleges: [], // 行政学院
          subjectOptions: [], // 专业
          collegeOptions: [],
          projectList: [],
          statusOptions: [
            {
              label: '待审核',
              value: 'Process'
            },
            {
              label: '已驳回',
              value: 'Reject'
            },
            {
              label: '已通过',
              value: 'End'
            }
          ]
        },
        breadPath: {
          label: '审核任务书',
          key: '1',
          children: [
            {
              key: '2',
              label: '审核'
            },
            {
              key: '3',
              label: '查看'
            }
          ]
        },
        subjectSelectedData: [], // 勾选的列表数据
        dialogVisible: false,
        actionType: '',
        subjectType: '',
        // 批量驳回
        rejectDialogVisible: false,
        rejectForm: {},
        rejectRules: {
          remark: [{required: true, message: '请输入', trigger: 'blur'}]
        },
        basicInfoForm: {},
        studentInfo: {},
        progressData: [] //审核进度列表
      }
    },
    created() {
      this.queryForm.condition.grade = new Date().getFullYear() + '';
      this.getCollege()
      this.getSubjectNature()
      tools.initAllCollege(this.source.studentColleges);
      if(this.dataInfo&&this.dataInfo.no){
        this.actionType=this.$route.query.actionType;
        if(this.dataInfo.content.studentId){
          getSubjectType(this.dataInfo.content.studentId).then(res => {
            this.subjectType = res.data;
          })
        }
      }
    },
    mounted() {
      if(this.dataInfo&&this.dataInfo.no){
        this.basicInfoForm=this.dataInfo.content;
        this.studentInfo=this.dataInfo.content;
        this.basicInfoForm.flowNo=this.dataInfo.no;
        this.progressData=this.dataInfo.listApproveHistory;
      }else{
        this.searchList();
      }
    },
    methods: {
      handleSizeChange(size) {
        this.queryForm.pageSize_ = size;
        this.searchList()
      },
      handleCurrentChange(pageNum) {
        this.queryForm.pageNum_ = pageNum;
        this.searchList()
      },
      searchList() {
        getAuditList(this.queryForm).then(res => {
          if (res.code == 200) {
            this.taskList = res.data.list
            this.queryForm.total = res.data.total
          }
        })
      },
      reset() {
        this.queryForm = {
          pageNum_: 1,
          pageSize_: 20,
          condition: {
            grade: null,
            faculty: null,
            status: null,
            subjectProperties: null,
            projectName: null,
            studentName: null,
            subject: null
          }
        }
        this.queryForm.condition.grade = new Date().getFullYear() + '';
        this.getSubjectNature();
        this.searchList()
      },
      handleSelectionChange(val) {
        this.subjectSelectedData = val
      },
      handleGradeChange(val) {
        this.queryForm.condition.subjectId = null
        this.source.subjectOptions = []
        if (val && this.queryForm.condition.faculty) {
          this.handleCollegeChange(this.queryForm.condition.faculty)
        }
        this.getSubjectNature();
        this.searchList();
      },
      // 查学院
      getCollege() {
        this.api.getCollege().then(res => {
          this.source.collegeOptions = res
        })
      },
      // 选择毕业年份和学院查专业
      handleCollegeChange(val) {
        this.queryForm.condition.subjectId = ''
        this.source.subjectOptions = []
        if (val && this.queryForm.condition.grade) {
          const params = {
            grade: this.queryForm.condition.grade,
            college: val
          }
          this.api
          .getSubject(params)
          .then(res => {
            this.source.subjectOptions = res.data
          })
          .catch()
        }
      },
      // 查课题性质
      getSubjectNature() {
        const params = {
          year: this.queryForm.condition.grade
        }
        this.api
        .getNature(params)
        .then(res => {
          this.source.subjectNatureOptions = res.data
        })
        .catch()
      },
      // 审核
      onReview(row) {
        this.actionType = 'REVIEW'
        viewData(row.id).then(res => {
          this.basicInfoForm = res.data
          this.studentInfo = {
            studentId: res.data.studentId,
            studentName: res.data.studentName,
            faculty: res.data.facultyI18n,
            subject: res.data.subject,
            direction: res.data.direction
          }
        })
        getSubjectType(row.studentId).then(res => {
          this.subjectType = res.data
        })
        approvelDetail({"no": row.flowNo}).then(res => {
          if (res && res.data) {
            this.progressData = res.data.listApproveHistory
          }
        })
        this.indexKey = "2";
      },
      // 审核驳回
      topicReject(data) {
        auditSingle(
          data
        ).then(res => {
          if (res.code == 200) {
            this.$message({
              type: 'success',
              message: "驳回成功！"
            })
            this.searchList()
            this.indexKey = 1
          } else {
            this.$message({
              type: 'fail',
              message: "驳回异常！"
            })
          }
        })
      },
      // 审核通过
      topicPass(data) {
        auditSingle(
          data
        ).then(res => {
          if (res.code == 200) {
            this.$message({
              type: 'success',
              message: "审核成功！"
            })
            this.searchList()
            this.indexKey = 1
          } else {
            this.$message({
              type: 'fail',
              message: "审核失败！"
            })
          }
        })
      },
      // 审核取消
      topicCancel() {
        this.indexKey = "1"
      },
      // 查看
      onView(row) {
        this.actionType = 'VIEW_TEACHER'
        viewData(row.id).then(res => {
          this.basicInfoForm = res.data
          this.studentInfo = {
            studentId: res.data.studentId,
            studentName: res.data.studentName,
            faculty: res.data.facultyI18n,
            subject: res.data.subject,
            direction: res.data.direction
          }
        })
        getSubjectType(row.studentId).then(res => {
          this.subjectType = res.data
        })
        approvelDetail({"no": row.flowNo}).then(res => {
          if (res && res.data) {
            this.progressData = res.data.listApproveHistory
          }
        })
        this.indexKey = "3";
      },
      // 批量通过
      onBatchPass() {
        if (this.subjectSelectedData.length === 0) {
          this.$eduAlert('请选择数据', '提示')
          return false
        }
        for (var index = 0; index < this.subjectSelectedData.length; index++) {
          if (this.subjectSelectedData[index].status == "Reject"
            || this.subjectSelectedData[index].status == "End") {
            this.$eduAlert('请选择待审核状态的数据', '提示')
            return false
          }
        }
        this.$eduConfirm('您确定通过吗？', '提示').then(() => {
          var idList = []
          for (var index = 0; index < this.subjectSelectedData.length; index++) {
            idList.push(this.subjectSelectedData[index].flowNo)
          }
          var data = {
            idList: idList,
            desc: "",
            type: 1
          }
          console.info(data);
          auditBatch(data).then(res => {
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: "审核成功！"
              })
              this.searchList()
            }
          })
        })
      },
      // 批量驳回
      onBatchReject() {
        if (this.subjectSelectedData.length === 0) {
          this.$eduAlert('请选择数据', '提示')
          return false
        }
        for (var index = 0; index < this.subjectSelectedData.length; index++) {
          if (this.subjectSelectedData[index].status == "Reject"
            || this.subjectSelectedData[index].status == "End") {
            this.$eduAlert('请选择待审核状态的数据', '提示')
            return false
          }
        }
        this.rejectForm = {}
        this.rejectDialogVisible = true
        this.$nextTick(() => {
          this.$refs.rejectForm.clearValidate()
        })
      },
      onRejectClose() {
        this.rejectDialogVisible = false
        this.searchList()
      },
      // 批量驳回提交
      onRejectSubmit() {
        this.$refs.rejectForm.validate(valid => {
          if (valid) {
            var idList = []
            for (var index = 0; index < this.subjectSelectedData.length; index++) {
              idList.push(this.subjectSelectedData[index].flowNo)
            }
            var data = {
              idList: idList,
              desc: this.rejectForm.remark,
              type: 2
            }
            auditBatch(data).then(res => {
              if (res.code == 200) {
                this.$message({
                  type: 'success',
                  message: "驳回成功！"
                })
                this.onRejectClose()
              }
            })
          } else {
            return false
          }
        })
      },
      // 导出
      onExport() {
        var user = this.global.mockUser;
        if (null == user) {
          user = this.global.user;
        }
        var faculty = user.manageFaculty;
        if (faculty == null) {
          faculty = user.faculty;
        }
        var facultyArray = faculty.split(",");
        this.queryForm.condition.facultyList = facultyArray;
        var params = {
          vo: this.queryForm.condition,
          list: this.subjectSelectedData
        }
        exportData(params).then(res => {
          // this.delayRequest(res.key)
          tools.downloadFile(res.key, "审核任务书.zip", getExportStatus, this);
        })
      },
      // 查文件导出状态，等待导出
      async delayRequest(key) {
        this.loading = true
        await new Promise(resolve => setTimeout(resolve, 3000))
        getExportStatus(key)
        .then(res => {
          this.loading = false
          if (res.code === 200 && res.data.status) {
            //let path = 'api/designservice/common/export?path=' + res.data.path
            let path = 'api/designservice/resultPublish/downLoadMore?path=' + res.data.path
              + "&fileName=docTask.zip";
            window.open(path)
          } else {
            this.delayRequest(key)
          }
        })
        .catch(err => {
          this.loading = false
          this.delayRequest(key)
        })
      }
    },
    components: {
      TaskInfo: () => import('../components/taskInfo')
    }
  }
</script>
<style lang="scss" scoped>
  .review-topic {
    .search-item {
      width: 100%;
    }
  }
</style>
